/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Property/IPDISTBRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace PDISTB
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id  ID   = 0;          ///< Unique identification number
        SG::DataStructure::Common::I64 TYPE = 0;          ///< Property entry type 1:pshell 2:pcomp 3:pcompg 5:pbeam 6:pbeam3
                                                          ///< 7:pbar 8:prod 9:pshear 10:ptube
        SG::DataStructure::Common::Id  PID    = 0;        ///< Property identification number
        SG::DataStructure::Common::I64 PNAME  = 0;        ///< Field location number converted from property name
        SG::DataStructure::Common::I64 LOCNUM = 0;        ///< PCOMP[G]:numerical order of ply; PBEAM[3]:station #;
                                                          ///< PBCOMP:integraion pt.
        SG::DataStructure::Common::Real SCALE     = 0.0;  ///< Scaling of distribution entries
        SG::DataStructure::Common::Real OFFSET    = 0.0;  ///< Offset of distribution entries
        SG::DataStructure::Common::I64  POS       = 0;    ///< Start position of ELEM data in the ELEM dataset
        SG::DataStructure::Common::I64  LEN       = 0;    ///< Length of ELEM data in the ELEM dataset
        SG::DataStructure::Common::Id   DOMAIN_ID = 0;    ///<
    };
};  // namespace PDISTB
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PDISTB::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "TYPE", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "PID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "PNAME", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "LOCNUM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SCALE", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "OFFSET", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::PDISTBData::ELEMData> ()
{
    HighFive::CompoundType ct = { { "EID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "VAL", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "VALI", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class PDISTBRepository : public IPDISTBRepository, public MainlyRepositoryBase<SG::DataStructure::FEM::PDISTBData>
{
    using MainStruct    = SG::DataStructure::FEM::PDISTBData;
    using SubStruct     = SG::DataStructure::FEM::PDISTBData::ELEMData;
    using NasMainStruct = SG::DBManager::HDF5::PDISTB::IDENTITY;

  public:
    explicit PDISTBRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wxc
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief    Get the All Data object
     *
     * @return
     * @author   wxc
     * @date     2024.12.25
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&         own,
                       std::vector<NasMainStruct>& _OUT IDENTITYData,
                       std::vector<SubStruct>& _OUT     subData,
                       bool                             isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>& IDENTITYData, std::vector<SubStruct>& subData, std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
